//
//  GYBannerView.m
//  GoodLuck
//
//  Created by apple on 2018/5/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *bannerCellIdentifier = @"bannerCellIdentifier";

#import "GYBannerView.h"
#import "GYBannerLayout.h"
#import "GYBannerCollectionViewCell.h"

@interface GYBannerView ()<UICollectionViewDelegate,UICollectionViewDataSource>

/** collectionView */
@property (nonatomic, strong) UICollectionView *collectionView;

/** 定时器 */
@property (nonatomic, strong) NSTimer *timer;

/** 当前移动index */
@property (nonatomic, assign) NSInteger index;
@end
@implementation GYBannerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.index = 0;
        //初始化
        [self setupCollectionView];
    }
    return self;
}

- (void)setupCollectionView {
    
    //实例化一个自定义Flowlayout
    GYBannerLayout *flowlayout = [[GYBannerLayout alloc]init];
    
    //实例化创建一个CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowlayout];
    
    //设置数据源代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    
    //注册Cell
    [collectionView registerNib:[UINib nibWithNibName:@"GYBannerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:bannerCellIdentifier];
    
    //添加到控制器上
    [self addSubview:collectionView];
    self.collectionView = collectionView;
}

- (void)setModel:(GYHome *)model {
    _model = model;
    //刷新视图
    [self.collectionView reloadData];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateOffset) userInfo:nil repeats:YES];
}

- (void)updateOffset {
    if (self.model.home_banners.count > 1) {
        if (self.index < self.model.home_banners.count-1) {
            self.index += 1;
            //1.得到当前偏移量
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
            //获取当前的偏移量
            CGFloat offsetX = self.collectionView.contentOffset.x;
            
            [self.collectionView setContentOffset:CGPointMake(offsetX + 260, 0) animated:YES];
            //[self.collectionView setContentOffset:CGPointMake(260*self.index+ (self.index==1 ? 0 :self.index*20), 0) animated:YES];
        }else {
            self.index = 0;
            [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
        }
    }
}

//行
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.home_banners.count;
}
//内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GYBannerCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:bannerCellIdentifier forIndexPath:indexPath];
    cell.model = self.model.home_banners[indexPath.item];
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.bannerClickCallBack(self.model.home_banners[indexPath.item]);
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始滑动的时候....");
//    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束滑动的时候....");
//    if (self.model.home_banners.count>1) {
//        [self startTimer];
//    }
}
- (void)startTimer {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changedOffset) userInfo:nil repeats:YES];
}

- (void)stopTimer {
    [self.timer invalidate];
    self.timer = nil;
}

- (void)changedOffset {
    //1.获取当前collectionView的偏移量
    CGFloat offsetX = self.collectionView.contentOffset.x;
//    int index = offsetX/ScreenWidth;
//    CGFloat value = ABS(offsetX- index*ScreenWidth);
//    if (index < self.model.home_banners.count-1) {
//
//    }
//    if (value < ScreenWidth/2) {
        //当前显示是index的item
//        currentIndex = index;
//    }else {
//        //当前显示是在index+1的item
//        currentIndex = index+1;
//    }
    
    //int index = 260*self.index+ (self.index==1 ? 0 :self.index*20)
//    if (index < self.model.home_banners.count-1) {
//        //1.得到当前偏移量
//        [self.collectionView setContentOffset:CGPointMake(260*index+ (index==1 ? 0 :index*20), 0) animated:YES];
//    }else {
//        [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
//    }
//    if (currentIndex < self.model.home_banners.count-1) {
//        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
//    }else {
//        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
//    }
}

@end
